#!/usr/bin/python
import os

from bs4 import BeautifulSoup as bs

url = "http://www.whatismyip.com.tw/"
url1 = "https://www.iwencai.com/stockpick/search?typed=1&preParams=&ts=1&f=3&qs=pc_~soniu~stock~stock~history~query&selfsectsn=&querytype=&searchfilter=&tid=stockpick&w=%E9%BE%99%E8%99%8E%E6%A6%9C"
# res = req.urlopen(url1)
# 读取页面数据，设置编码格式
file = 'D:/test.txt'
os.path.exists(file)
res = open(file, encoding='utf-8')
data = res.read()
#解析html界面
soup = bs(data, 'html.parser')
#遍历所有一级标签
body = soup.body
result = {
    "data":{}
}
# result[soup.title.name] = soup.title.string
table = body.find("table", "static_table tbody_table static_tbody_table")
tbody = table.tbody
string = ""
n = 0
tr = tbody.find_all("tr")
for it in tr:
    tds = {}
    td = it.find_all("td")
    tds["id"] = td[0].find("div").string
    tds["secu_code"] = td[2].find("div").string
    tds["secu_name"] = td[3].find("a").string
    result["data"][n] = tds
    n = n +1
print(result)